package com.hmily.spring.samples.scanner;

import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.Proxy;

/**
 * <h1>Mapper的实例类。</h1>
 *
 * @author hmilyylimh
 * ^_^
 * @version 0.0.1
 * ^_^
 * @date 2021-10-31
 */
public class MapperFactoryBean<T> implements FactoryBean<T> {

    private Class rawMapperClass;

    public MapperFactoryBean() {
    }

    public MapperFactoryBean(Class rawMapperClass) {
        this.rawMapperClass = rawMapperClass;
    }

    @Override
    public T getObject() throws Exception {
        Object proxy = Proxy.newProxyInstance(
                rawMapperClass.getClassLoader(),
                new Class[]{rawMapperClass},
                new MapperProxy()
        );
        return (T) proxy;
    }

    @Override
    public Class<?> getObjectType() {
        return rawMapperClass;
    }
}
